const { v4: uuidv4 } = require('uuid');

/**
 * 订单类 - 代表投资者的交易指令
 */
class Order {
  /**
   * 创建订单实例
   * @param {string} investorId - 投资者ID
   * @param {string} assetId - 资产ID
   * @param {string} type - 订单类型 ('buy' 或 'sell')
   * @param {number} price - 订单价格
   * @param {number} quantity - 订单数量
   * @param {Object} config - 其他配置选项
   */
  constructor(investorId, assetId, type, price, quantity, config = {}) {
    this.id = uuidv4();
    this.investorId = investorId;
    this.assetId = assetId;
    this.type = type; // 'buy' 或 'sell'
    this.price = price;
    this.quantity = quantity;
    this.timestamp = Date.now();
    this.status = 'open'; // 'open', 'filled', 'partially_filled', 'cancelled'
    this.filledQuantity = 0;
    this.config = {
      orderType: config.orderType || 'limit', // 'limit' 或 'market'
      ...config
    };
  }

  /**
   * 更新订单状态
   * @param {string} status - 新状态
   * @param {number} filledQuantity - 已成交数量
   */
  updateStatus(status, filledQuantity = 0) {
    this.status = status;
    this.filledQuantity += filledQuantity;
  }

  /**
   * 取消订单
   */
  cancel() {
    this.status = 'cancelled';
  }

  /**
   * 获取订单剩余数量
   * @returns {number} 剩余数量
   */
  getRemainingQuantity() {
    return this.quantity - this.filledQuantity;
  }
}

module.exports = Order;